package com.elves.auth.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1")
public class ApiAction {

    @GetMapping(value = "/jobs")
    @ResponseBody
    public String getJobs(Model model) {
        log.info("model:{}", model);
        return "jobs";
    }

    @GetMapping(value = "/job/{id}")
    @ResponseBody
    public String getJobById(Model model, @PathVariable("id") Integer id) {
        log.info("model:{}", model);
        return "job:" + id;
    }

    /**
     * @param model
     * @param head  标题
     * @param name  产品
     * @param data  参数
     * @return
     */
    @PostMapping(value = "/go")
    @ResponseBody
    public String goSee(Model model, @RequestParam("head") String head, @RequestParam("name") String name, @RequestParam("data") String data) {
        log.info("model:{}", model);
        log.info("name:{}", name);
//        return "see.html";
        String seeHtml = """
                <!DOCTYPE html>
                <html lang="zh-CN">
                    <head>
                        <meta charset="UTF-8">
                        <title>""";
        seeHtml = seeHtml + head;
        seeHtml = seeHtml + """
                        </title>
                        <style>
                            body { font-family: Arial, sans-serif; margin: 2rem; }
                              header {
                                        position: fixed;
                                        top: 0;
                                        left: 0;
                                        width: 100%;
                                        background-color: #f8f8f8;
                                        padding: 10px;
                                        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
                                        z-index: 1000;
                                    }
                
                                    main {
                                                margin-top: 160px; /* 与header高度相同 */
                                                margin-bottom: 60px; /* 与footer高度相同 */
                                                padding: 10px;
                                                flex: 1;
                                            }
                
                            h1 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 0.5rem; }
                            h2 { color: #34495e; margin-top: 1.5rem; }
                            table { width: 100%; border-collapse: collapse; margin: 1rem 0; }
                            th, td { padding: 12px; border: 1px solid #ddd; text-align: left; }
                            th { background-color: #f8f9fa; }
                            .highlight { background-color: #f1f8ff; }
                        </style>
                    </head>
                    <body> 
                    <header>
                        <h1>
                """;
        seeHtml = seeHtml + head + """
                </h1>""";
        log.info("name:{}", name);
        seeHtml = seeHtml + """
                <h2 style="margin-left: 10px;">
                """ + name + "</h2>";
        seeHtml = seeHtml + """
                </header> <main>""";

        seeHtml = seeHtml + """
                <table>""";
        // 将data字符串按换行符拆分为列表
        List<String> strList = Arrays.asList(data.split("\\r?\\n"));
        log.info("str size:{}", strList.size());
        for (int i = 0; i < strList.size(); i++) {
            String line = strList.get(i);
            String[] msg = line.split("\t");
            String key = msg[0];
            String val = "";
            if (msg.length > 1) {
                val = msg[1];
            }
            log.info("index:{}  key:{}  val:{}", i, key, val);
            // 如果i是奇数，为tr标签加上class="highlight"属性
            if (i % 2 == 1) {
                seeHtml = seeHtml + """
                        <tr class="highlight">""";
            } else {
                seeHtml = seeHtml + """
                        <tr>""";
            }
            seeHtml = seeHtml + """
                    <th>""";
            seeHtml = seeHtml + key;
            seeHtml = seeHtml + """
                    </th>""";

            seeHtml = seeHtml + """
                    <td>""";
            seeHtml = seeHtml + val;
            seeHtml = seeHtml + """
                    </td>""";
            seeHtml = seeHtml + """
                    <tr>""";
        }
        seeHtml = seeHtml + """
                </table>""";
        seeHtml = seeHtml + """ 
                      </main>
                      </body>
                </html>
                """;
        return seeHtml;
    }

}
